
cfgCatFormUi = Ext.extend(Ext.form.FormPanel, {

	region: 'center',
	autoScroll: true,
	style: 'overflow-x: hidden;',
	height: 460,
	id: 'cfgCatForm',
	title: 'Configurador de Catalogos',
	frame: true,
	border: false,
	defaultType: 'textfield',
    initComponent: function(arguments) {
    	
        this.items = [
			{
				xtype: 'tbspacer',
				height: 10
			},
            {
            	xtype: 'container',
            	layout: 'column',
            	items:[
            	{
            		xtype: 'fieldset',
			    	style: 'padding: 10px',
            		title: 'Configuraci&oacute;n General',
					defaultType: 'textfield',
            		items: [
			            {
			                fieldLabel: 'titulo',
							maskRe     : /[a-z ]/i,
							anchor: '100%',
							id: 'nbTitle',
			                allowBlank:false
			            },
						new Ext.form.ComboBox({
			            	fieldLabel: 'esquema',
				            id:'cmbSchema',
				            triggerAction: 'all',
				            width: 200,
			   				valueField: 'id',
			    			displayField: 'name',
			                allowBlank:false,
				            store: new Ext.data.JsonStore({
			   					url: 'admCat.do?method=getCombo&tpCombo=schemas',
			   					root: 'data',     
			  					fields: ['id','name'] 
							}),
							listeners:{ 
							'select' : function(cmb,record,index){ 
								     tableCmb.clearValue();
								     tableCmb.enable();   
								     tableDs.load({          
								         params:{  
								             schema:record.get('id')  
								         }  
								     });  
								 }
							}
				        }),
						tableCmb = new Ext.form.ComboBox({
							disabled: true, 
			            	fieldLabel: 'tabla',
			            	mode: 'local',       
			                allowBlank:false,
				            id:'cmbTable',
				            triggerAction: 'all',
				            width: 200,
			   				valueField: 'id',
			    			displayField: 'name',
				            store: tableDs = new Ext.data.JsonStore({
			   					url:  'admCat.do?method=getCombo&tpCombo=tables',
			   					root: 'data',     
			  					fields: ['id','name'] 
							}),
							listeners:{ 
							'select' : function(cmb,record,index){
									
									secuenciaCmb.enable();
								    secuenciaDs.load({          
								         params:{  
								             table:record.get('id')  
								         }  
								     });  
									
									camposCmb.enable();
								    camposDs.load({          
								         params:{  
								             table:record.get('id')  
								         }  
								     });  
								}
							}
				        }),	        
						secuenciaCmb = new Ext.form.ComboBox({
							disabled: true, 
			            	fieldLabel: 'secuencia',
			            	mode: 'local',      //Step 2  
				            id:'cmbSec',
				            triggerAction: 'all',
				            width: 200,
			   				valueField: 'id',
			    			displayField: 'name',
				            store: secuenciaDs = new Ext.data.JsonStore({
			   					url:  'admCat.do?method=getCombo&tpCombo=sequences',
			   					root: 'data',     
			  					fields: ['id','name'] 
							}),
							listeners:{ 
							'select' : function(){
								}
							}
				        }),	        
				        {
			                xtype: 'tbspacer',
			            	height: 5
			            },
			            {
			                xtype: 'button',
							style: 'padding-left: 170px;',
			                text:'agregar configuraci&oacute;n',
			                iconCls: 'silk-accept',
							handler: function(){
								var x = Ext.getCmp('nbTitle').getValue();
								if(x != ""){
									Ext.Ajax.request({
										method: 'POST',
									   	url: '/agvWeb/admCat.do?method=addConfiguration',
										   success: function (response,conn){
												var responseData = Ext.util.JSON.decode(response.responseText);
												if(responseData.success){
													listViewStore.load();
													clearForm();
											   		Ext.Msg.show({
											            			 	title:'agv software',
											            			 	msg: 'El catalogo ha sido configurado exitosamente.', 
											            			 	buttons: Ext.Msg.OK, 
											            			 	icon: 'silk-accept' });
												} else {
											   		Ext.Msg.show({
											            			 	title:'agv software',
											            			 	msg: responseData.msg, 
											            			 	buttons: Ext.Msg.OK, 
											            			 	icon: 'silk-error' });
												}
										   },
									   params: { title: Ext.getCmp('nbTitle').getValue(),
									   					schema:  Ext.getCmp('cmbSchema').getValue(),
									   					table:  Ext.getCmp('cmbTable').getValue(),
									   					sequence: Ext.getCmp('cmbSec').getValue()
									   					}
									});
								} else {
									   		Ext.Msg.show({
									            			 	title:'agv software',
									            			 	msg: 'Debe informar los campos requeridos', 
									            			 	buttons: Ext.Msg.OK, 
									            			 	icon: 'silk-error' });
								}
							}
			            }
            		]
            	},
				{
				    xtype: 'box',
			    	height: 250,
				    autoEl: {tag: 'img', src:'/agvWeb/ext/images/icons/fam/arrow_right_2.png'}
				},
				    {
				    	xtype: 'fieldset',
				    	width: 400,
				    	style: 'padding: 10px',
				    	layout: 'form',
				    	id:'paramForm',
				    	title: 'Configuraci&oacute;n de Parametros',
						defaultType: 'textfield',
						labelWidth: 150,
				    	items: [
								camposCmb = new Ext.form.ComboBox({
								disabled: true, 
            					fieldLabel: 'campo',
            					mode: 'local',     
	            				id:'cmbcampos',
	            				triggerAction: 'all',
	            				width: 200,
   								valueField: 'id',
    							displayField: 'name',
	            				store: camposDs = new Ext.data.JsonStore({
   									url:  'admCat.do?method=getCombo&tpCombo=fields',
   									root: 'data',     
  									fields: ['id','name','type','lenght'] 
								}),
								listeners:
									{ 
										'select' : function(cmb,record,index){
											Ext.getCmp('dataType').setValue(record.data.type);
											Ext.getCmp('dataLenght').setValue(record.data.lenght);
										}
									}
	       	 					}),	        
				    		{
								disabled: true, 
            					allowBlank: false,
				            	fieldLabel: 'tipo de dato',
				            	id:'dataType',
				            	width: 200,
				            	id: 'dataType'
				    		},
				    		{
								disabled: true, 
            					allowBlank: false,
				            	fieldLabel: 'longitud',
				            	id: 'dataLenght'
				    		},
				    		{
				            	fieldLabel: 'alias',
				            	id: 'alias'
				    		},
							new Ext.form.ComboBox({
            					fieldLabel: 'tipo de validaci&oacute;n',
	            				id:'cmbVtypes',
	            				triggerAction: 'all',
	            				width: 200,
   								valueField: 'id',
    							displayField: 'name',
	            				store: vtypesDs = new Ext.data.JsonStore({
   									url:  'admCat.do?method=getCombo&tpCombo=vtypes',
   									root: 'data',     
  									fields: ['id','name'] 
								}),
								listeners:
									{ 
										'select' : function(cmb,record,index){
										}
									}
	       	 					}),
		    				{
				            	fieldLabel: 'reg ex validaci&oacute;n',
	            				id:'regexVal',
				                allowBlank:true
		    				},
	       	 				{
								xtype: 'checkbox',
	            				id:'idField',
				            	fieldLabel: '&iquest; es id ?'
				    		},
	       	 				{
								xtype: 'checkbox',
	            				id:'nbAutoComplete',
				            	fieldLabel: '&iquest; considerar para autocompletar ?'
				    		},
				    		{
								xtype: 'checkbox',
	            				id:'req',
				            	fieldLabel: '&iquest; es obligatorio ?'
				    		},
				    		{
								xtype: 'checkbox',
	            				id:'dupVal',
				            	fieldLabel: '&iquest; validar duplicidad ?'
				    		},
				    		{
								xtype: 'checkbox',
	            				id:'otTable',
				            	fieldLabel: '&iquest; viene de otra tabla ?',
				                listeners: {     
				                		check: function (ctl, val) {
						            		if(val) 
						             			Ext.getCmp('catTableCont').show();
						             		else 
						              			Ext.getCmp('catTableCont').hide();
				                		} 
				                }
				    		},
				    		{
								xtype: 'fieldset',
								title: 'Relaci&oacute;n con otra tabla',
								layout: 'form',
					    		defaults: {autoScroll: true}, 
				    			hidden: true,
				    			autoScroll: true,
				    			id: 'catTableCont',
								defaultType: 'textfield',
				    			items:[
									new Ext.form.ComboBox({
						            	fieldLabel: 'esquema',
							            id:'otNbSchema',
							            triggerAction: 'all',
							            width: 200,
						   				valueField: 'id',
						    			displayField: 'name',
						                allowBlank:false,
							            store: new Ext.data.JsonStore({
						   					url: 'admCat.do?method=getCombo&tpCombo=schemas',
						   					root: 'data',     
						  					fields: ['id','name'] 
										}),
										listeners:{ 
										'select' : function(cmb,record,index){ 
											     otTableCmb.clearValue();
											     otTableCmb.enable();   
											     otTableDs.load({          
											         params:{  
											             schema:record.get('id')  
											         }  
											     });  
											 }
										}
							        }),
									otTableCmb = new Ext.form.ComboBox({
										disabled: true, 
						            	fieldLabel: 'tabla',
						            	mode: 'local',       
						                allowBlank:false,
							            id:'nbTable',
							            triggerAction: 'all',
							            width: 200,
						   				valueField: 'id',
						    			displayField: 'name',
							            store: otTableDs = new Ext.data.JsonStore({
						   					url:  'admCat.do?method=getCombo&tpCombo=tables',
						   					root: 'data',     
						  					fields: ['id','name'] 
										}),
										listeners:{ 
										'select' : function(cmb,record,index){
												otCamposCmb.enable();
											    otCamposDs.load({          
											         params:{  
											             table:record.get('id')  
											         }  
											     });  
											     
												otCamposCmb2.enable();
											    otCamposDs2.load({          
											         params:{  
											             table:record.get('id')  
											         }  
											     });  
											     
											     
											}
										}
							        }),	        
									otCamposCmb = new Ext.form.ComboBox({
									disabled: true, 
	            					fieldLabel: 'campo',
	            					mode: 'local',     
		            				id:'nbField',
		            				triggerAction: 'all',
		            				width: 200,
	   								valueField: 'id',
	    							displayField: 'name',
		            				store: otCamposDs = new Ext.data.JsonStore({
	   									url:  'admCat.do?method=getCombo&tpCombo=fields',
	   									root: 'data',     
	  									fields: ['id','name','type','lenght'] 
									}),
									listeners:
										{ 
											'select' : function(cmb,record,index){
											}
										}
		       	 					}),	        
									otCamposCmb2 = new Ext.form.ComboBox({
									disabled: true, 
	            					fieldLabel: 'campo',
	            					mode: 'local',     
		            				id:'nbLegend',
		            				triggerAction: 'all',
		            				width: 200,
	   								valueField: 'id',
	    							displayField: 'name',
		            				store: otCamposDs2 = new Ext.data.JsonStore({
	   									url:  'admCat.do?method=getCombo&tpCombo=fields',
	   									root: 'data',     
	  									fields: ['id','name','type','lenght'] 
									}),
									listeners:
										{ 
											'select' : function(cmb,record,index){
											}
										}
		       	 					})
				    			]
				    		},
				    		{
								xtype: 'button',
								style: 'padding-left: 240px;',
								text: 'agregar parametro',
								iconCls: 'silk-arrow-right',
								handler: function(){
									var x = Ext.getCmp('cmbcampos').getValue();
									if(x != ""){
										Ext.Ajax.request({
											method: 'POST',
										   	url: '/agvWeb/admCat.do?method=addParameters',
											   success: function (response,conn){
													var responseData = Ext.util.JSON.decode(response.responseText);
													
													if(responseData.success){
														listViewStore.load();
														clearParamForm();
													    camposDs.load({          
													         params:{  
													             table:Ext.getCmp('cmbTable').getValue(),
													             addParam: true
													         }  
													     });  
													} else {
												   		Ext.Msg.show({
												            			 	title:'agv software',
												            			 	msg: responseData.msg, 
												            			 	buttons: Ext.Msg.OK, 
												            			 	icon: 'silk-error' });
													}
											   },
										   params: { field: Ext.getCmp('cmbcampos').getValue(),
										   					alias: Ext.getCmp('alias').getValue(),
										   					dataType:  Ext.getCmp('dataType').getValue(),
										   					lenght:  Ext.getCmp('dataLenght').getValue(),
										   					valType: Ext.getCmp('cmbVtypes').getValue(),
										   					regexVal: Ext.getCmp('regexVal').getValue(),
										   					id: Ext.getCmp('idField').getValue(),
										   					required: Ext.getCmp('req').getValue(),
										   					dupVal: Ext.getCmp('dupVal').getValue(),
										   					otTable: Ext.getCmp('otTable').getValue(),
										   					nbTable: Ext.getCmp('otNbSchema').getValue() + "." + Ext.getCmp('nbTable').getValue(),
										   					nbField: Ext.getCmp('nbField').getValue(),
										   					nbAutoComplete: Ext.getCmp('nbAutoComplete').getValue(),
										   					nbLegend: Ext.getCmp('nbLegend').getValue()}
										});
									} else {
										   		Ext.Msg.show({
										            			 	title:'agv software',
										            			 	msg: 'Debe informar los campos requeridos', 
										            			 	buttons: Ext.Msg.OK, 
										            			 	icon: 'silk-error' });
									}
								}
				    		}
				    	]
				    },
				{
				    xtype: 'box',
			    	height: 250,
				    autoEl: {tag: 'img', src:'/agvWeb/ext/images/icons/fam/arrow_right_2.png'}
				},
				
				    {
				    	xtype: 'fieldset',
				    	width: 400,
				    	style: 'padding: 10px',
				    	title: 'Parametros agregados',
			    		bbar: new Ext.Toolbar({
			    			buttonAlign: 'right',
			    			items:[
					        	{xtype: 'tbseparator'},
				    			{
									text: 'modificar',
									iconCls: 'silk-user-edit',
									handler: 
										function(){
											var x = listView.selected.elements.length;
											if(x == 0){
												Ext.Msg.show({
										            			 	title:'agv software',
										            			 	msg: 'Debe seleccionar un parametro', 
										            			 	buttons: Ext.Msg.OK, 
										            			 	icon: 'silk-error' });
											} else {
												var selectedItem = listView.selected.elements[0];
											}
										}					    				
				    			},
					        	{xtype: 'tbseparator'},
				    			{
									text: 'eliminar',
									iconCls: 'silk-delete',
									handler: 
										function(){
											var x = listView.selected.elements.length;
											if(x == 0){
												Ext.Msg.show({
										            			 	title:'agv software',
										            			 	msg: 'Debe seleccionar un parametro', 
										            			 	buttons: Ext.Msg.OK, 
										            			 	icon: 'silk-error' });
											} else {
												var y = listView.getSelectedRecords();
												var nm = y[0].data.name;

												Ext.Ajax.request({
													method: 'POST',
												   	url: '/agvWeb/admCat.do?method=deleteParameters',
													   success: function (response,conn){
															var responseData = Ext.util.JSON.decode(response.responseText);
															
															if(responseData.success){
																listViewStore.load();
															} else {
														   		Ext.Msg.show({
														            			 	title:'agv software',
														            			 	msg: responseData.msg, 
														            			 	buttons: Ext.Msg.OK, 
														            			 	icon: 'silk-error' });
															}
													   },
												   params: { field: nm}
												});
											}
										}					    				
				    			},
					        	{xtype: 'tbseparator'}
			    			]
			    		}),
				    	items:[
				    		listView = new Ext.list.ListView({
				    		singleSelect: true,
				    		autoScroll: true,
				    		reserveScrollOffset: true,
				    		store:  listViewStore = new Ext.data.JsonStore({
							    url: '/agvWeb/admCat.do?method=getParameters',
							    root: 'data',
							    fields: ['name', 'type']
							}),
							id: 'listView',
							columns: [{
						        header: 'nombre',
						        dataIndex: 'name'
						    },{
						        header: 'tipo',
						        dataIndex: 'type'
						    }]
						})
				    	]
				    }				
				
				
				]
            
            }];
        
			Ext.Ajax.request({
				method: 'POST',
			   	url: '/agvWeb/admCat.do?method=cleanParameters'
			});
            
        cfgCatFormUi.superclass.initComponent.call(this);
        
        
		function clearForm(){
			Ext.getCmp('cmbcampos').setValue('');
			Ext.getCmp('alias').setValue('');
			Ext.getCmp('dataType').setValue('');
			Ext.getCmp('dataLenght').setValue('');
			Ext.getCmp('cmbVtypes').setValue('');
			Ext.getCmp('regexVal').setValue('');
			Ext.getCmp('idField').setValue('');
			Ext.getCmp('req').setValue('');
			Ext.getCmp('dupVal').setValue('');
			Ext.getCmp('otTable').setValue('');
			Ext.getCmp('otNbSchema').setValue('');
			Ext.getCmp('nbTable').setValue('');
			Ext.getCmp('nbField').setValue('');
			Ext.getCmp('nbLegend').setValue('');
  			Ext.getCmp('nbTitle').setValue('');
			Ext.getCmp('cmbSchema').setValue('');
			Ext.getCmp('cmbTable').setValue('');
			Ext.getCmp('cmbSec').setValue('');
			Ext.getCmp('nbAutoComplete').setValue('');
		}
		
		function clearParamForm(){
			Ext.getCmp('cmbcampos').setValue('');
			Ext.getCmp('alias').setValue('');
			Ext.getCmp('dataType').setValue('');
			Ext.getCmp('dataLenght').setValue('');
			Ext.getCmp('cmbVtypes').setValue('');
			Ext.getCmp('regexVal').setValue('');
			Ext.getCmp('idField').setValue('');
			Ext.getCmp('req').setValue('');
			Ext.getCmp('dupVal').setValue('');
			Ext.getCmp('otTable').setValue('');
			Ext.getCmp('otNbSchema').setValue('');
			Ext.getCmp('nbTable').setValue('');
			Ext.getCmp('nbField').setValue('');
			Ext.getCmp('nbLegend').setValue('');
			Ext.getCmp('nbAutoComplete').setValue('');
		}
		
    }

});

cfgCatForm = Ext.extend(cfgCatFormUi, {
    initComponent: function(arguments) {
        cfgCatForm.superclass.initComponent.call(this);
    }
});


